<?php
// error_reporting(0);		//关闭所有PHP错误报告
session_start();
$pwd = '8866';
if($_SESSION['login_key'] != $pwd){
	if($_POST['login_key'] == $pwd){
		$_SESSION['login_key'] = $pwd;
	}else{
		header('refresh:1;url=login.php');
		exit('先登录！');
	}
}

$param = $_GET['param'];
$dir = "./esps";

// 新增ESP操作
if($param == 'add'){
	$esp = $_GET['esp'];
	if(!file_exists($dir)){mkdir($dir, 0777, true);}
	$add_dir = $dir . '/' . $esp;
	mkdir($add_dir, 0777, true);
	$handle1 = fopen($add_dir . '/ret.txt', "w+");
	$write1 = fwrite($handle1, '1');
	$handle2 = fopen($add_dir . '/calltime.txt', "w+");
	$write2 = fwrite($handle2, date('Y-m-d H:i:s'));
	$handle3 = fopen($add_dir . '/note.txt', "w+");
	$write3 = fwrite($handle3, "无备注");
	header('location:index.php');
	// var_dump($arr);
	// var_dump($num_dir);
}

//登出
if($param == 'loginout'){
	unset($_SESSION['login_key']);
	header('refresh:1;url=login.php');
	exit('先登录！');
}

//修改某个ESP电平值的操作
if($param == 'update'){
	$esp = $_GET['esp'];
	$val = $_GET['val'];
	$handle3 = fopen($dir . '/' . $esp . '/ret.txt', "w+");
	$write3 = fwrite($handle3, $val);
}

//删除某个ESP电平值的操作
if($param == 'del'){
	$esp = $_GET['esp'];
	$del_dir = $dir . '/' . $esp;
	// 确保目录路径是正确的
	if (is_dir($del_dir)) {
		// 获取目录下所有的文件
		$files = glob($del_dir . '/*');
	 
		// 删除所有文件
		foreach ($files as $file) {
			if (is_writable($file)) {
				unlink($file);
			} else {
				echo "文件 {$file} 不可写，无法删除。\n";
			}
		}
		rmdir($del_dir);
	} else {
		echo "目录 {$del_dir} 不存在。";
	}
	header('location:index.php');
}
//给某个ESP修改备注
if($param == 'donote'){
	$esp = $_GET['esp'];
	$note = $_GET['note'];
	$handle4 = fopen($dir . '/' . $esp . '/note.txt', "w+");
	$write4 = fwrite($handle4, $note);
	header('location:index.php');
}

$arr_esps = [];
$arr_dir = scandir($dir);
$now = time();
foreach ($arr_dir as $v) {
	if ($v != "." && $v != "..") {
		if(is_dir($dir . '/' . $v)){
			$calltime = file_get_contents($dir . '/' . $v . '/calltime.txt');
			$time = strtotime($calltime);
			$note = file_get_contents($dir . '/' . $v . '/note.txt');
			
			$arr_esps[$v]['ret'] = file_get_contents($dir . '/' . $v . '/ret.txt');
			$arr_esps[$v]['calltime'] = $calltime;
			$arr_esps[$v]['status'] = $now - $time < 10 ? '在线' : '【离线】';
			$arr_esps[$v]['note'] = $note;
		}
	}
}

// echo "<pre>";
// print_r($arr_esps);
// echo "</pre>";
?>
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>ESP01s管理</title>
</head>
<style>
.main{
	background: rgba(255, 255, 255, 0.8);
	margin: 16px 16px 24px 16px;
	padding: 24px;
	box-shadow: 1px 1px 10px 1px rgba(0,0,0,0.2);
}
.box-buttons-lr {
    display: flex;
}
.box-buttons-l {
    flex: 1;
}
.box-buttons-r {
}
.esps td{
	text-align: center;
}
</style>
<body>
<div class="main">
	<div class="box-buttons-lr">
		<div class="box-buttons-l">
			<button type="button" onclick="loginout()">点我!</button>
		</div>
		<div class="box-buttons-r">
			<button type="button" onclick="add()">新增</button>
		</div>
	</div>
	<hr style="border: none; border-top: 1px solid #E6E6E6;"/>
	<table class="esps" border="1" width="100%" cellspacing=0 cellpadding=8>
		<tr>
			<th>ESP01s序号</th>
			<th>当前ret值</th>
			<th>最后通讯时间</th>
			<th>状态</th>
			<th>备注</th>
			<th>操作</th>
		</tr>
<?php
foreach($arr_esps as $k=>$v){
?>
		<tr>
			<td><?php echo $k;?></td>
			<td><?php echo $v['ret'];?></td>
			<td><?php echo $v['calltime'];?></td>
			<td><?php echo $v['status'];?></td>
			<td><?php echo $v['note'];?></td>
			<td>
				<button type="button" onclick="do_note(`<?php echo $k;?>`)">改备注</button>
				<button type="button" onclick="update(`<?php echo $k;?>`, '0')">发送0</button>
				<button type="button" onclick="update(`<?php echo $k;?>`, '1')">发送1</button>
				<button type="button" onclick="del(`<?php echo $k;?>`)">删除</button>
			</td>
		</tr>
<?php
}
?>
	</table>
	
</div>

</body>
<script>
function add(){
	var esp = prompt("请输入ESP序号（如d3，d6等等）");
	if (esp != null && esp != ""){
		location='index.php?param=add&esp='+esp;
	}else{
		alert("必须输入有效的目录！");
	}
}
function update(esp, val){
	// alert(esp + val);
	location='index.php?param=update&esp='+esp+'&val='+val;
}
function loginout(){
	location='index.php?param=loginout';
}
function del(esp){
	if(confirm("确定删除吗？")){
		location='index.php?param=del&esp='+esp;
	}else{
		alert("您选择了取消。");
	}
}
function do_note(esp){
	var note = prompt("请输入ESP备注：");
	if (note != null && note != ""){
		location='index.php?param=donote&esp='+esp+'&note='+note;
	}else{
		alert("必须输入有效的备注（简短的名字就可以）！");
	}
}
</script>
</html>